<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1. 构造函数
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }
        Person.prototype.sayName = function () {
            console.log('大家好，我的名字是' + this.name + '，今年' + this.age + '岁了');
        }
        let person1 = new Person('张三', 18);
        let person2 = new Person('李四', 20);
        // 2. 对象字面量
        let person3 = {
            name: '王五',
            age: 22,
            gender: '男',
            girlfriend: {
                name: '小红',
                age: 18,
            },
            sayName: function () {
                console.log('大家好，我的名字是' + this.name + '，今年' + this.age + '岁了');
                if (this.gender === '男') {
                    console.log('我的性别是' + this.gender +
                        ',这是我的女朋友' + this.girlfriend.name + ',她今年' + this.girlfriend.age + '岁了');
                }
                else {
                    console.log('我的性别是' + this.gender +
                        ',这是我的男朋友' + this.girlfriend.name + ',他今年' + this.girlfriend.age + '岁了');
                }

            }
        }
        let person4 = {
            name: '赵六',
            age: 24,
            gender: '女',
            friend: {
                name: '小明',
                age: 18,
            },
            sayName: function () {
                console.log('大家好，我的名字是' + this.name + '，今年' + this.age + '岁了');
                if (this.gender === '男') {
                    console.log('我的性别是' + this.gender +
                        ',这是我的女朋友' + this.friend.name + ',她今年' + this.friend.age + '岁了');
                }
                else {
                    console.log('我的性别是' + this.gender +
                        ',这是我的男朋友' + this.friend.name + ',他今年' + this.friend.age + '岁了');
                }

            }
        }
        person1.sayName();
        person2.sayName();
        person3.sayName();
        person4.sayName();

    </script>
</body>

</html>